package cn.eng.product.service.impl;

import cn.eng.basic.service.IBaiduAiAuditService;
import cn.eng.basic.util.AjaxResult;
import cn.eng.basic.util.LoginContext;
import cn.eng.org.domain.Employee;
import cn.eng.org.mapper.EmployeeMapper;
import cn.eng.product.domain.Product;
import cn.eng.product.domain.ProductDetail;
import cn.eng.product.domain.ProductOnlineAuditLog;
import cn.eng.product.mapper.ProductDetailMapper;
import cn.eng.product.mapper.ProductMapper;
import cn.eng.product.mapper.ProductOnlineAuditLogMapper;
import cn.eng.product.service.IProductService;
import cn.eng.basic.service.impl.BaseServiceImpl;
import cn.eng.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;


    @Override
    public void save(Product product) {
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail!=null){
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        //根据product_id去删除t_product_detail的数据
        productDetailMapper.delProductDetailByProductId(id);
    }

    @Override
    public void update(Product product) {
        super.update(product);
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail !=null){
            productDetailMapper.updateProductDetail(product);
        }
    }

    @Override
    public void patchDel(Long[] ids) {
        super.patchDel(ids);
        for (Long id:ids){
            remove(id);
        }
    }

    /**
     * 上架 业务
     * @param ids
     * @param request
     */
    @Override
    public AjaxResult onsale(Long[] ids, HttpServletRequest request) {
        Logininfo logininfo =  LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        for (Long id:ids) {
            Product product = productMapper.loadById(id);
            //1.审核 - 智能审核
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            if (boo1 && boo2){//审核通过
                //2.审核通过：修改状态state=1，上架时间 - 添加审核日志
                productMapper.updateOnSaleState(id,new Date());
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(1);
                log.setNote("审核成功");
                log.setAuditId(employee.getId());
                productOnlineAuditLogMapper.save(log);
            }else {
                //3.审核失败：添加审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(id);
                log.setState(0);
                log.setNote("审核失败");
                log.setAuditId(employee.getId());
                productOnlineAuditLogMapper.save(log);
                return AjaxResult.me().setSuccess(false).setMsg("上架失败");
            }

        }
        return new AjaxResult();
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public void offsale(Long[] ids) {
        //1.修改状态，修改下架的时间
        productMapper.updateState(ids,new Date());
    }
}
